<!--
2018-05-23修改活动
新人专享0.1元骑7天
已兼容小程序
-->
<template>
  <div class="container" v-title data-title="新人专享8.8元骑7天">
    <div class="suite">
      <div class="thumbImg">
        <img :src="require('../styles/asset/nine/nine_07.png')" />
      </div>
      <div class="thumbImg">
        <img :src="require('../styles/asset/nine/nine_08.png')" />
      </div>
    </div>
    <div class="coupon1">
      <div><img :src="require('../styles/asset/nine/nine_06.png')" /><a v-on:click="buySuite(productId)"> </a></div>
    </div>
    <simplert ref="simplert"></simplert>
  </div>
</template>

<script>
var wx = require('weixin-js-sdk');
  import Simplert from '../../components/dialog/dialog.vue';
  let filters = {
    parseURL:function(url) {
      let a = document.createElement('a');
      a.href = url;
      return {
        params: (function () {
          let ret = {},
            param = a.href.split('?')[1],
            seg = param.split('&'),
            len = seg.length,
            i = 0,
            s;
          for (; i < len; i++) {
            if (!seg[i]) {
              continue;
            }
            s = seg[i].split('=');
            ret[s[0]] = s[1];
          }
          return ret;
        })()
      }
    }
  };

  export default {
    data () {
      return {
        productId:'',
        token:"",
        state:true,
        postState:false,
        img: require('../styles/asset/share.jpg'),
        id:'',
        wx_app:''
      }
    },
    created () {

    },
    mounted(){
      const that = this;
      const url = window.location.href;
      this.id = filters.parseURL(url).params.id;
      if(url.indexOf('wx_app')>0){
        this.wx_app = filters.parseURL(url).params.wx_app;
        this.detail = filters.parseURL(url).params.detail;
        if(this.detail == 'true'){
          this.orderId = filters.parseURL(url).params.orderId;
        }
      }else{
        this.wx_app = ''
      }
      this.$util.getUserToken().then(token=>{
        that.token = token;
        that.getSuiteId();
      })
      let root = process.env.MAIN_ROOT;
      this.$util.sendShareMessage("六个轮子 | 新人专享8.8元骑7天>>","六个轮子 | 新人专享8.8元骑7天>>", root+this.img,url);
      if(url.indexOf('wx_app')>0){
        let newUrl = this.$util.wxUrl(url);
        // 小程序分享title，url
        wx.miniProgram.postMessage({
          data: {
            title: '新人专享8.8元骑7天',
            wx_url: newUrl
          }
        })
      }
    },
    components: {
      Simplert
    },
    methods: {
      open (title, confirmBtn,customConfirmBtnText,onConfirm,type,onClose,message) {
        let obj = {
          title: title,
          useConfirmBtn: confirmBtn,
          customConfirmBtnText: customConfirmBtnText,
          onConfirm:onConfirm,
          type: type,
          onClose:onClose,
          message:message
        }
        this.$refs.simplert.openSimplert(obj)
      },
      getSuiteId(){
        let that = this;
        that.$api.post('/activity/v1/authApi/getIdsByActivity?id=' + this.id,{},s=>{
          s.module.forEach(function(e){
            that.productId = e.id
          })
        },f=>{
          that.open('套餐信息获取失败',false,'','','fade_alert',function(){

          });
        },{
          'requestPlatform': that.wx_app,
          'token': that.token
        })
      },
      buySuite(pid){
        if(!this.wx_app){
            let userAgentObj = navigator.userAgent;
            if(userAgentObj.match(/MicroMessenger/i)){
              //$(".weiChat").show();
              if(userAgentObj.match(/Android/i)){
                window.location.href = 'http://app.qq.com/#id=detail&appid=1106220919'
              }else if(userAgentObj.match(/iPhone/i)) {
                window.setTimeout(function(){
                  window.location.href = "https://itunes.apple.com/cn/app/%E7%94%B5%E6%96%91%E9%A9%AC-%E8%AE%A9%E5%87%BA%E8%A1%8C%E6%9B%B4%E5%AE%89%E5%85%A8/id1252928655?mt=8";
                },200)
              }else{
                window.location.href="http://download.xingjiakeji.com/";
              }
              return;
            }else{
              if(!this.token || this.token == undefined || this.token == ""){
                window.setTimeout(function(){
                  if(userAgentObj.match(/Android/i)){
                    window.location.href = 'http://app.qq.com/#id=detail&appid=1106220919';
                  }else if(userAgentObj.match(/iPhone/i)) {
                    window.location.href = "https://itunes.apple.com/cn/app/%E7%94%B5%E6%96%91%E9%A9%AC-%E8%AE%A9%E5%87%BA%E8%A1%8C%E6%9B%B4%E5%AE%89%E5%85%A8/id1252928655?mt=8";
                  }
                },1000);
                return;
              }
            }
        }
        if(this.postState){
          return
        }else{
          this.postState = true;
        }
        const that = this;
        this.open('您是否确认购买此套餐',true,'确认',function(){
          that.$api.post('/product/v1/authApi/buy?productId=' + pid,{},s=>{
            that.postState = false;
            let userAgentObj = navigator.userAgent;
            that.open('购买成功',false,'','','fade_success',function(){
              //兼容小程序
              if(that.wx_app){
                  if(that.detail == 'true'){
                    wx.miniProgram.reLaunch({url: '../detail/detail?orderId=' + that.orderId})
                  }
              }else{
                if(userAgentObj.match(/Android/i)){
                  window.control.refreshWallet();
                }else if(userAgentObj.match(/iPhone/i)){
                  refreshWallet();
                }
                that.$router.go(-1)
              }
            });
          },f=>{
            that.postState = false;
            if(f.resultCode == '30006')
            {
              that.open('余额不足请先充值！',true,'充值',function(){
                let userAgentObj = navigator.userAgent;
                //兼容小程序
                if(that.wx_app){
                  wx.miniProgram.navigateTo({url: '../user/balance/balance?detail=true'})
                }else{
                  if(userAgentObj.match(/Android/i)){
                    window.control.recharge();
                  }else if(userAgentObj.match(/iPhone/i)){
                    recharge();
                  }
                }
              })
            }else if(f.errorMessage){
              that.open(f.errorMessage,false,'','','fade_alert',function(){});
            }else{
              that.open('购买失败',false,'','','fade_alert',function(){});
            }
          },{
            'requestPlatform': that.wx_app,
            'token': that.token
          })
        },'',function(){
          that.postState = false;
        },"")
      },
    }
  }
</script>
<style scoped >
  @import "../styles/styles.css";
  .container{  width: 100%; height: 100%; }
  .suite{  margin: 0 auto; max-width: 750px; height: 100%; overflow-y: scroll; padding-bottom: 2.05rem;}
  .thumbImg>img{ width: 100%;}
  .thumb{ padding-bottom:0.2rem;}
  .thumb>img{ width: 100%;}
  .coupon1 img{ width: 100%;}
  .coupon1>div{ position: relative;}
  .coupon1>div>a{ position: absolute; width: 100%; height: 1rem; left:0; top: 0.75rem;}
  .coupon1{ padding: 0; position: absolute; bottom: 0; width: 100%;  z-index: 99;}
</style>
